<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 获取缩略图地址
 *
 * @package    helper
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_View_Helper_Thumb extends EGP_View_Helper_Abstract
{

    /**
     * 初始化方法
     *
     * @param  string   $imageLink  图片地址
     * @param  integer  $maxWidth   最大宽度
     * @param  integer  $maxHeight  最大高度
     * @return string
     */
    public function init($imageLink, $maxWidth, $maxHeight = null)
    {
        if (empty($imageLink)) {
            return '';
        }

        $config = EGP_Core::loadConfig('app');
        $maxWidth  = (integer) $maxWidth;
        $maxHeight = (integer) $maxHeight;

        if ($maxWidth > 10) {
            $extension = strtolower(substr(strrchr($imageLink, '.'), 1));
            $dirname = str_ireplace(
                str_replace(EGP_WEB_DIR, '', $config->upload->basePath),
                str_replace(EGP_WEB_DIR, '', $config->thumb->basePath),
                dirname($imageLink)
            );
            $basename = basename($imageLink, $extension);
            if ($maxHeight > 0) {
                $imageLink = "{$dirname}/{$basename}({$maxWidth}x{$maxHeight}).$extension";
            } else {
                $imageLink = "{$dirname}/{$basename}({$maxWidth}).$extension";
            }
        }

        return $config->baseSite . ltrim($imageLink, '/');
    }

}